import { Middleware, IMiddleware } from "@midwayjs/core";
import { NextFunction, Context } from "@midwayjs/koa";
import { BigInt } from "../interface"
import { AjaxResult, MapUtils } from "../util";

@Middleware()
export class ReportMiddleware implements IMiddleware<Context, NextFunction> {
  resolve() {
    return async (ctx: Context, next: NextFunction) => {
      // 控制器前执行的逻辑
      const startTime = Date.now();
      // 执行下一个 Web 中间件，最后执行到控制器
      // 这里可以拿到下一个中间件或者控制器的返回值
      // console.log(ctx.query);
      
      const result = await next();

      (BigInt as unknown as BigInt).prototype.toJSON = function () { return this.toString() }

      if (result) {
        // 控制器之后执行的逻辑
        ctx.logger.info(
          `Report in "src/middleware/report.middleware.ts", rt = ${Date.now() - startTime
          }ms ${JSON.stringify(result)}`
        );
      } else {
        // 控制器之后执行的逻辑
        ctx.logger.info(
          `Report in "src/middleware/report.middleware.ts", rt = ${Date.now() - startTime
          }ms`
        );
      }
      if (result instanceof AjaxResult) {
        return MapUtils.strMapToObj(result);
      }else{
        return result
      }
      // 返回给上一个中间件的结果
    };
  }

  static getName(): string {
    return "report";
  }
}
